home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / SunSoft.CafeDelSol / ScrollMap / scroll < prev    next >
Text File  |  1996-02-22  |  15KB  |  499 lines

  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.net.*;
  4. import java.net.MalformedURLException;
  5. import java.util.StringTokenizer;
  6.  
  7. class ImageLoader extends Thread {
  8.         ScrollMap parent;
  9.         Image img;
  10.         
  11.         ImageLoader (ScrollMap parent) {
  12.                 this.parent = parent;
  13.                 start();
  14.                 }
  15.         public void run() {
  16.     parent.debug("img");    
  17.         int n = 0;
  18.                 while (n < (parent.nimgs -1)) {
  19.         n++;
  20.         img = parent.getImage (parent.getDocumentBase(), parent.img_dir + "/" + parent.urlarr[n]);
  21.  
  22.                   if (parent.numimgarr[n] != 0) {
  23.             parent.animgarr[n] = new MyImage( parent, n, parent.numimgarr[n], parent.urlarr[n]);
  24.           }
  25.  
  26.  
  27.  
  28.         parent.ldimage =  false;
  29.                 parent.add(img, n);
  30.            }
  31.         }
  32. }
  33.  
  34. class MyImage extends Thread {
  35.  
  36.     Image animg[] ;
  37.     ScrollMap parent;
  38.     String url;
  39.  
  40.     int N;
  41.     int which = 1;
  42.     int skip = 0;
  43.  
  44. public void run() {
  45.      for ( int nn=1;nn < (parent.numimgarr[N] + 1);nn++) {
  46.          animg[nn] = parent.getImage (parent.getDocumentBase(), parent.img_dir + "/" + parent.urlarr[N] + nn);
  47.         //System.out.println(parent.numimgarr[N] + " " + parent.urlarr[N] + nn);
  48.      }
  49. }
  50.  
  51. MyImage ( ScrollMap parent, int n, int nimgs, String url) {
  52.  
  53. this.parent = parent;
  54. N = n;
  55. this.url = url;
  56. animg = new Image[nimgs+1];
  57.  
  58. start();
  59. }
  60.  
  61. public class ScrollMap extends java.applet.Applet implements Runnable {
  62.  
  63.     Image offscrImg;
  64.     Graphics offscrG; 
  65.  
  66.     boolean onarr[];
  67.  
  68.     Image imgs[];
  69.     MyImage animgarr[];
  70.  
  71.     String param; 
  72.     String prev; 
  73.     String current; 
  74.  
  75.     String urlarr[] = new String [64];
  76.     String destarr[] = new String [64];
  77.     String namearr[] = new String [64];
  78.     String soundarr[] = new String [64];
  79.  
  80.     int numimgarr[] = new int [64];
  81.     int skiparr[] = new int [64];
  82.     int backarr[] = new int [64];
  83.     int widtharr[] = new int [64];
  84.     int accum[] = new int [64];
  85.     int num = 1;
  86.     int n;
  87.     int pos;
  88.     int fix;
  89.     int newpos;
  90.     int nimgs;
  91.     int width;
  92.     int height;
  93.     int totalwidth = 0;  
  94.     int x, newx;
  95.     int dist = 1;
  96.     int timeout = 200;
  97.     int mousepos;
  98.  
  99.     Thread scroller = null;
  100.  
  101.     String img_dir;
  102.     String showstat;
  103.     String before;
  104.     String after;
  105.     String url;
  106.     String dir;
  107.     String statuschk;
  108.  
  109.     boolean ldimage = false;
  110.     boolean loaded;
  111.     boolean top;
  112.     boolean in = false;
  113.  
  114.     URL anchor;
  115.  
  116.  
  117.     public void debug (String s) {
  118.     System.out.println(s);
  119.     }
  120.    
  121.     public void init () {
  122.  
  123. debug("init");
  124.     String at = getParameter("before");
  125.     before = (at != null) ? at : "";
  126.     at = getParameter("after");
  127.     after = (at != null) ? at : "";
  128.     at = getParameter("img_dir");
  129.     img_dir = (at != null) ? at : "doc:/demo/images/duke";
  130.     at = getParameter("speed");
  131.     timeout = 1000 / ((at == null) ? 4 : Integer.valueOf(at).intValue());
  132.     at = getParameter("dir");
  133.     dist = (at == null) ? 5 : Integer.valueOf(at).intValue();
  134.         width = size().width;
  135.         height = size().height;
  136.  
  137.     newx = x = size().width;
  138.  
  139.         while (true) {   
  140.                 nimgs++;
  141.  
  142.         urlarr[nimgs] =  getParameter("url"+nimgs);
  143.         destarr[nimgs] = getParameter("dest"+nimgs);
  144.         namearr[nimgs] = getParameter("name"+nimgs);
  145.         soundarr[nimgs] = getParameter("sound"+nimgs);
  146.                 if (getParameter("background"+nimgs) != null) {
  147.                 backarr[nimgs] = Integer.valueOf(getParameter("background"+nimgs)).intValue();
  148.                 } else {
  149.                 backarr[nimgs] = 0;
  150.                 }
  151.             if (getParameter("skip"+nimgs) != null) {
  152.         skiparr[nimgs] = Integer.valueOf(getParameter("skip"+nimgs)).intValue(); 
  153.         } else {
  154.         skiparr[nimgs] = 0;
  155.         }
  156.             if (getParameter("numimg"+nimgs) != null) {
  157.         numimgarr[nimgs] = Integer.valueOf(getParameter("numimg"+nimgs)).intValue();
  158.         } else {
  159.         numimgarr[nimgs] = 0;
  160.         }
  161.                 if (urlarr[nimgs] == null) {
  162.                 break;
  163.                 }
  164.         }        
  165.     onarr = new boolean[nimgs+1];
  166.     imgs = new Image[nimgs+1];
  167.         animgarr = new MyImage[nimgs];
  168.  
  169.                 offscrImg = createImage(width, height);
  170.                 offscrG = offscrImg.getGraphics();
  171.  
  172.     }
  173.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
  174.         if ((flags & WIDTH) != 0 ) {
  175.       n++;
  176.       if (n >= (nimgs-1) ){
  177.         for (n=1;n < nimgs; n++) {
  178.             int tmpW = imgs[n].getWidth(this);
  179.             totalwidth +=  tmpW;
  180.             widtharr[n] = tmpW;
  181.         }
  182.       }
  183.         }
  184.         return super.imageUpdate(img, flags, x, y, w, h);
  185.     }
  186.     public void showDocument(URL u) {
  187.         getAppletContext().showDocument(u);
  188.     }
  189.  
  190.     public synchronized void add(Image img, int n) {
  191.         imgs[n] = img;
  192.         
  193.         if (isActive()) {
  194.             repaint();
  195.         }
  196.         }
  197.  
  198.     public void start() {
  199.         if (!loaded) {
  200.             loaded = true;
  201.             new ImageLoader(this);
  202.         }
  203.     if (scroller == null) {
  204.         scroller = new Thread(this);
  205.         scroller.start();
  206. System.out.println("start");
  207.     }
  208.     }
  209.     public void stop() {
  210.         if (scroller != null && scroller.isAlive()) {
  211.             scroller.stop();
  212. System.out.println("stop");
  213.         }
  214.  
  215.     loaded = false;
  216.         scroller = null;
  217.     }
  218.  
  219.  
  220.     public void run() {
  221.     while (true) {
  222.     try {Thread.currentThread().sleep(timeout);} catch (InterruptedException e){}
  223.         scroll(dist);
  224.     }
  225.     }
  226.  
  227.     synchronized void scroll(int dist) {
  228.     repaint();
  229.     }
  230.  
  231.     public void update(Graphics g) {
  232.         newx += dist;
  233.     g.setColor(Color.lightGray);
  234.  
  235.     if (newx != x) {
  236.         int dist = newx - x;
  237.         if (dist > 0) {
  238.         for (x = newx ; x > width ; x -= Math.max(width - 2, totalwidth));
  239.         paint(g, 1, dist + 1);
  240.         } else {
  241.         for (x = newx ; x < 0 ; x += Math.max(width - 2, totalwidth));
  242.         paint(g, (width-1) + dist, width-1);
  243.         }
  244.         if ( in ) {
  245.         statusShow();
  246.         }
  247.     } else {
  248.         //paint(g);
  249.     }
  250.     }
  251.     public void paint(Graphics g, int fromx, int tox) {
  252.     paintbuffer(offscrG, fromx, tox);
  253.     g.drawImage(offscrImg, 0, 0, this);
  254.     }
  255.         
  256.  
  257.     public void paintbuffer(Graphics g, int fromx, int tox) {
  258.     int x = this.x;
  259.     newx = x;
  260.         fix = x;
  261.     int h = 0;
  262.     int w = 0;
  263.         int accum =0;
  264.         int tmpwidth =0;
  265.  
  266.     g.setColor(Color.lightGray);
  267.     g.fillRect(1, 1, width, height-2);
  268.     g.clipRect(1, 1, width, height-2);
  269.  
  270.  
  271.     for (int i = 0 ; i < nimgs ; i++) {
  272.         if (imgs[i] == null) {
  273.         continue;
  274.         }
  275.         w = imgs[i].getWidth(this);
  276.         h = imgs[i].getHeight(this);
  277.  
  278.  
  279.               if ( numimgarr[i] != 0 ) {
  280.                 if (backarr[i] != 0) {
  281.                 g.drawImage(imgs[i], x, height - (h+1), this);
  282.                 }
  283.                 if (animgarr[i].skip == skiparr[i] ) {
  284.                 animgarr[i].skip = 0;
  285.                 g.drawImage(animgarr[i].animg[animgarr[i].which], (x - tmpwidth), height - (h+1), this);     
  286.                 animgarr[i].which++;
  287.                 } else {    
  288.                 g.drawImage(animgarr[i].animg[animgarr[i].which], (x - tmpwidth), height - (h+1), this);     
  289.                 animgarr[i].skip++;
  290.                 }
  291.                 if (animgarr[i].which > numimgarr[i]) {
  292.                 animgarr[i].which = 1;
  293.                 }
  294.  
  295.               } else {
  296.                 g.drawImage(imgs[i], x, height - (h+1), this);
  297.               }  
  298.  
  299.             if ((x + w) > width) {
  300.                 x -= Math.max(width - 2, totalwidth);
  301.             }
  302.                 x += w;
  303.         }
  304. /*
  305. System.out.println( x + " ");
  306.                   for (int t = 1;t<nimgs+1;t++) {
  307.           tmpwidth = widtharr[t];
  308.           accum += tmpwidth; 
  309.  
  310.                    if (numimgarr[t] != 0) {
  311.                      g.drawImage(animgarr[t].animg[animgarr[t].which], (x - tmpwidth), height - (h+1), this);          
  312.                     animgarr[t].which++;
  313.                     if (animgarr[t].which > numimgarr[t]) {
  314.                         animgarr[t].which = 1;
  315.                     }
  316.                     }
  317.                    }
  318.  
  319. */
  320.         
  321.     }
  322.  
  323.  
  324.     public void paint(Graphics g) {
  325.  
  326.     //offscrG.draw3DRect(0, 0, width, height, true);
  327.     paint(g, 1, width-1);
  328.     }
  329.  
  330.    public void statusShow (){
  331.  
  332.        showStatus( showstat ) ;
  333.        accum[0] = 0;
  334.        pos = (-fix);
  335.        newpos = ((totalwidth - fix) - (totalwidth - width));
  336.        for (int i = 0 ;i < (nimgs - 1) ; i++) {
  337.  
  338.          if (pos < 0 ) {
  339.                 if ((width -mousepos) < newpos) {
  340.                     accum[0] = (accum[0] + widtharr[i]);
  341.                     if ( (newpos - (width - mousepos)) >= (accum[0]) && (newpos - (width - mousepos ) ) <= (accum[0]+widtharr[i+1])){
  342.             current = urlarr[i+1];
  343.             if (!current.equals(prev)) { onarr[i+1] = false;} else { onarr[i+1] = true;} 
  344.             if (soundarr[i+1] != null && !onarr[i+1]) { play(getCodeBase(), soundarr[i+1]);}
  345.  
  346.             if (destarr[i+1] != null ) {
  347.             if (namearr[i+1] !=null ) {
  348.                                 showstat = "Click to select: " + namearr[i+1];
  349.             } else {
  350.                             if ( destarr[i+1].equals("same")) { 
  351.                                 showstat = "Click to select: " + urlarr[i+1];
  352.                             } else {   
  353.                                 showstat = "Click to select: " + destarr[i+1];
  354.                             } 
  355.             }
  356.                 } else {
  357.                showstat = " ";        
  358.                     }            
  359.                     break;
  360.                    }
  361.                 } else {
  362.                     accum[0] = (accum[0] + widtharr[i]);
  363.                     if ( ( (totalwidth - fix) + mousepos  >= accum[0] ) &&  ( (totalwidth - fix) + mousepos  <= (accum[0] + widtharr[i+1]) ) ) {
  364.             current = urlarr[i+1];
  365.             if (!current.equals(prev)) { onarr[i+1] = false;} else { onarr[i+1] = true;} 
  366.             if (soundarr[i+1] != null && !onarr[i+1]) { play(getCodeBase(), soundarr[i+1]);}
  367.                     if (destarr[i+1] != null) { 
  368.                         if (namearr[i+1] != null ) { 
  369.                                 showstat = "Click to select: " + namearr[i+1];
  370.                         } else { 
  371.                     if ( destarr[i+1].equals("same")) {
  372.                                 showstat = "Click to select: " + urlarr[i+1];
  373.                 } else {
  374.                                 showstat = "Click to select: " + destarr[i+1];
  375.                             }                     
  376.                         } 
  377.                     } else {             
  378.                        showstat = " ";  
  379.                     } 
  380.                     break;
  381.                 }
  382.          }
  383.        } else {
  384.                    accum[0] = (accum[0] + widtharr[i]);
  385.                    if ( (pos + mousepos) >= (accum[0]) && (pos + mousepos ) <= (accum[0]+widtharr[i+1])){
  386.             current = urlarr[i+1];
  387.             if (!current.equals(prev)) { onarr[i+1] = false;} else { onarr[i+1] = true;} 
  388.             if (soundarr[i+1] != null && !onarr[i+1]) { play(getCodeBase(), soundarr[i+1]);}
  389.                     if (destarr[i+1] != null) { 
  390.                         if (namearr[i+1] != null ) { 
  391.                                 showstat = "Click to select: " + namearr[i+1];
  392.                         } else { 
  393.                             if ( destarr[i+1].equals("same") ) { 
  394.                                 showstat = "Click to select: " + urlarr[i+1];
  395.                             } else {   
  396.                                 showstat = "Click to select: " + destarr[i+1];
  397.                             } 
  398.                         } 
  399.                     } else {             
  400.                        showstat = " ";  
  401.                     } 
  402.                         break;
  403.                     }
  404.                 }
  405.         }
  406.  
  407.     prev = current;
  408.         return;
  409.     }
  410.  
  411.     public boolean handleEvent(Event evt) {
  412.     boolean mouse = true;
  413.     if (evt.id == Event.MOUSE_DOWN) {
  414.         
  415.        int mdx = evt.x;
  416.  
  417.        accum[0] = 0;
  418.        pos = (-fix);
  419.        newpos = ((totalwidth - fix) - (totalwidth - width));
  420.        for (int i = 0 ;i < (nimgs -1) ; i++) {
  421.  
  422.          if (pos < 0 ) {
  423.                 if ((width -mdx) < newpos) {
  424.                     accum[0] = (accum[0] + widtharr[i]);
  425.                     if ( (newpos - (width - mdx)) >= (accum[0]) && (newpos - (width - mdx ) ) <= (accum[0]+widtharr[i+1])){
  426.             if ( destarr[i+1] == null ) { continue; }
  427.             if ( destarr[i+1].equals("same") ) { 
  428.                        try {
  429.                        anchor = new URL(null,  before + urlarr[i+1] + after);
  430.                        } catch (MalformedURLException e) {
  431.                        anchor = null;
  432.                        }
  433.                     } else {
  434.                        try { 
  435.                        anchor = new URL(null,  destarr[i+1]);showStatus("Fetching ...");showDocument(anchor);
  436.                        } catch (MalformedURLException e) { 
  437.                        anchor = null; 
  438.                        }
  439.                     }
  440.                    break;
  441.                    }
  442.                 } else {
  443.                     accum[0] = (accum[0] + widtharr[i]);
  444.                     if ( ( (totalwidth - fix) + mdx  >= accum[0] ) &&  ( (totalwidth - fix) + mdx  <= (accum[0] + widtharr[i+1]) ) ) {
  445.                     if ( destarr[i+1] == null ) { continue; }
  446.                     if ( destarr[i+1].equals("same") ) { 
  447.                        try {  
  448.                        anchor = new URL(null,  before + urlarr[i+1] + after);showStatus("Fetching ...");showDocument(anchor); 
  449.                        } catch (MalformedURLException e) {  
  450.                        anchor = null; 
  451.                        } 
  452.                     } else {
  453.                        try {   
  454.                        anchor = new URL(null,  destarr[i+1]);showStatus("Fetching ...");showDocument(anchor); 
  455.                        } catch (MalformedURLException e) {   
  456.                        anchor = null;  
  457.                        } 
  458.                     }
  459.                     break;
  460.                 }
  461.          }
  462.        } else {
  463.                    accum[0] = (accum[0] + widtharr[i]);
  464.                    if ( (pos + mdx) >= (accum[0]) && (pos + mdx ) <= (accum[0]+widtharr[i+1])){
  465.                     if ( destarr[i+1] == null ) { continue; }
  466.                     if ( destarr[i+1].equals("same") ) { 
  467.                         try {    
  468.                         anchor = new URL(null,  before + urlarr[i+1] + after);showStatus("Fetching ...");showDocument(anchor); 
  469.                         } catch (MalformedURLException e) {    
  470.                         anchor = null;  
  471.                         } 
  472.                         } else {
  473.                         try {    
  474.                         anchor = new URL(null,  destarr[i+1]);showStatus("Fetching ...");showDocument(anchor); 
  475.                         } catch (MalformedURLException e) {     
  476.                         anchor = null;   
  477.                         }  
  478.                       }
  479.                         break;
  480.                     }
  481.                 }
  482.         }
  483.  
  484.  
  485.         }
  486.           if (evt.id == Event.MOUSE_MOVE) {
  487.             in = true;
  488.             mousepos = evt.x;
  489.           }
  490.           if (evt.id == Event.MOUSE_EXIT) {
  491.             showStatus( " ") ;
  492.         in = false;
  493.           }
  494.           return mouse;
  495.         }
  496.  
  497. }
  498.